package com.huawei.org.service.impl;

import com.huawei.basic.exception.BusinessException;
import com.huawei.basic.utils.BaiduAuditUtils;
import com.huawei.org.domain.Employee;
import com.huawei.org.domain.Shop;
import com.huawei.org.domain.ShopAuditLog;
import com.huawei.org.mapper.EmployeeMapper;
import com.huawei.org.mapper.ShopAuditLogMapper;
import com.huawei.org.mapper.ShopMapper;
import com.huawei.org.service.IShopService;
import com.huawei.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raintea
 * @since 2022-05-03
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

        @Autowired
        private ShopMapper shopMapper;

        @Autowired
        private EmployeeMapper employeeMapper;
        
        @Autowired
        private JavaMailSender javaMailSender;
        
        @Autowired
        private ShopAuditLogMapper shopAuditLogMapper;
        
    @Override
    @Transactional
    public void settlement(Shop shop) {
        //校验
        String name = shop.getName();
        if(StringUtils.isEmpty(name)){
            throw new BusinessException("店铺名字不能为空！！！！");
        }
        // 百度智能审核
        if (!BaiduAuditUtils.TextCensor(name)){
            throw new BusinessException("能不能好好取名字？不能就爬");
        }
        // 店铺不能重复注册
        Shop shopTmp = shopMapper.loadByName(name);
        if(shopTmp != null){
            throw new BusinessException("店铺已存在，请直接登陆，如果忘记密码，请找回密码！！");
        }
        // 账户是否已经被注册
        Employee admin = shop.getAdmin();
        Employee employeeTmp = employeeMapper.loadByUserName(admin.getUsername());
        //已经注册了用户了
        if(employeeTmp != null && employeeTmp.getShop_id() != null){
            throw new BusinessException("该账号已经绑定过店铺，请直接登陆！！");
        }
        //需要找到一个账号来绑定
        if(employeeTmp == null){
            employeeMapper.save(admin);//返回自增ID
            employeeTmp = admin;//新增的员工赋值给employeeTmp
        }

        shop.setAdminId(employeeTmp.getId());
        shopMapper.save(shop);//返回自增Id
        employeeTmp.setShop_id(shop.getId());
        employeeMapper.update(employeeTmp);
    }

    /**
     * 驳回
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //1.把店铺的状态设置
        Shop shop = shopMapper.loadById(Long.valueOf(shopAuditLog.getShopId()));
        shopMapper.update(shop);
        //2.记录日志
        shopAuditLog.setAuditId(327);
        shopAuditLogMapper.save(shopAuditLog);
        //3、 发送邮件
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1037180303@qq.com");
        //邮件主题
        mailMessage.setSubject("系统指南");
        //邮件内容
        mailMessage.setText("<h1>审核失败</h1><br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>");
        //收件人
        mailMessage.setTo("1037180303@qq.com");
//        mailMessage.setTo(employee.getEmail());

        javaMailSender.send(mailMessage);
    }

    /**
     * 通过
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //1.把店铺的状态设置
        Shop shop = shopMapper.loadById(Long.valueOf(shopAuditLog.getShopId()));
        shopMapper.update(shop);
        //2.记录日志 @TODO ，
        shopAuditLog.setAuditId(327);
        shopAuditLogMapper.save(shopAuditLog);
        //3、 发送邮件
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1037180303@qq.com");
        //邮件主题
        mailMessage.setSubject(shop.getName() + "审核通过---激活邮件请接收");
        //邮件内容
        /* mailMessage.setText("1111");*/
        //邮件正文
        mailMessage.setText("亲 您的激活链接为："+
                "<a href='http://127.0.0.1:8080/shop/activation?shopId="+shopAuditLog.getShopId()+"'>点击进行激活资料</a>");
        //收件人
//        mailMessage.setTo(employee.getEmail());
        mailMessage.setTo("1037180303@qq.com");

        javaMailSender.send(mailMessage);
    }


}
